home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / mc302emb.zip / LIBCF / ATOI.ASM < prev    next >
Assembly Source File  |  1994-03-18  |  903b  |  40 lines

  1. *
  2. * Get a decimal number from a string
  3. *
  4. atoi    LDI    2,S        Get argument pointer
  5. * First, skip any whitespace preceeding the number
  6. ?1    LDB    I        Get char
  7.     CMPB    #' '        Is it space?
  8.     SJNZ    ?2        Yes, go to next
  9.     LDB    I        Get char back
  10.     CMPB    #9        Tab?
  11.     SJZ    ?3        No, its legit
  12. ?2    LEAI    1,I        Skip to next
  13.     SJMP    ?1        Try next
  14. * Keep track of '-' sign for negative numbers
  15. ?3    LDB    I        Get char
  16.     CMPB    #'-'        Is it negative?
  17.     SJZ    ?4        No, its OK
  18.     LEAI    1,I        Skip '-'
  19.     CALL    ?4        Get number
  20.     NEG            Negate
  21.     RET
  22. * Add up the digits into a binary value
  23. ?4    CLR            Begin with zero
  24.     ST    ?temp        Set parm
  25. ?5    LDB    I        Get char
  26.     LEAI    1,I        Skip to next
  27.     SUBB    #'0'        Convert
  28.     PUSHA            Save for later
  29.     CMPB    #9        In range
  30.     UGT            Test for OK
  31.     SJNZ    ?6        No, exit
  32.     LD    ?temp        Get old value
  33.     MULB    #10        Multiply
  34.     ADDB    S+        Include new number
  35.     ST    ?temp        Resave
  36.     SJMP    ?5        And proceed
  37. ?6    LD    S+        Fix stack
  38.     LD    ?temp        Get value
  39.     RET
  40.